package cqut.gbq.qimo.service.impl;

import cqut.gbq.qimo.entity.Course;
import cqut.gbq.qimo.entity.ResBean;
import cqut.gbq.qimo.entity.Student;
import cqut.gbq.qimo.mapper.CourseMapper;
import cqut.gbq.qimo.mapper.StudentMapper;
import cqut.gbq.qimo.service.StudentsService;
import cqut.gbq.qimo.utils.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @ClassName StucentServiceImpl
 * @Description TODO
 * @Author guobenqi
 * @Date 2021/6/16 14:14
 */

@Service
public class StudentServiceImpl implements StudentsService {

    @Autowired
    private StudentMapper studentMapper;

    @Autowired
    private CourseMapper courseMapper;

    @Override
    public List<Student> findAllStudent() {
        return studentMapper.findAllStudent();
    }

    @Override
    public Student login(String username, String password) {
        return studentMapper.findOneStudentByUsernameAndPassWord(username,password);
    }

    @Override
    public ResBean register(String username, String password, String school, String name,String stuId) {
//         Student student = studentMapper.findOneStudentByUsernameAndPassWord(username, password);
        Student student = studentMapper.findOneStudentByUsername(username);
         if (student != null){
             return ResBean.error("该学生已经注册");
         }else{
             Integer count = studentMapper.insertOneStudent(username,password,school,name,stuId);
             if (count > 0 ){
                 return ResBean.success("注册成功");
             }else{
                 return ResBean.error("注册失败");
             }
         }

//        return ResBean.error("注册失败");
    }

    @Override
    public List<Course> getClassInfo(Integer sid) {
        return studentMapper.getClassInfo(sid);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResBean joinCourse(Integer sid, String code) {
        Course course = courseMapper.codeIsExist(code);
        if (course == null){
            return ResBean.error("此课程码不存在,请重新输入!");
        }else{
            Integer courseId = course.getCid();

            int count  = studentMapper.joinCourse(sid,courseId);
//            根据cid查aid
            List<Integer> aidList = courseMapper.findCourseAid(courseId);

            for(Integer aid:aidList){
                System.out.println("课程作业aid:"+aid);
                int count2 = studentMapper.joinCourseAssginment(sid,aid);
            }

            if (count > 0){
                return ResBean.success("加入课程成功");
            }else{
                return ResBean.error("加入课程失败!");
            }
        }

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResBean dropCourse(Integer sid, Integer cid) {
        Integer count = studentMapper.dropCourse(sid,cid);
//        退课的所有作业
        List<Integer> aidList = courseMapper.findCourseAid(cid);
        for(Integer aid:aidList){
            int count2 = studentMapper.dropCourseAssginment(sid,aid);
        }
        if (count > 0){
            return ResBean.success("退课成功!");
        }
        return ResBean.error("退课失败");
    }

    @Override
    public ResBean getCourseCount(Integer cid) {

        return null;
    }

    @Override
    public ResBean getStuInfo(Integer sid) {
        Student student = studentMapper.getStuInfoById(sid);
        if (student != null) {
            return ResBean.success("获取学生信息成功！",student);
        }else{
            return ResBean.error("获取失败！");
        }
    }

    @Override
    public ResBean editStuInfo(Student student) {
        int count = 0;
        if (student.getPassword() == null){

            count = studentMapper.editStudent(student.getSid(),student.getStuId(),student.getSchool(),student.getName());
            if (count > 0){
                return ResBean.success("修改基本信息成功!");

            }else{
                return ResBean.error("修改基本失败！");
            }

        }else{
            String password = MD5Utils.MD5(student.getPassword());
            count = studentMapper.editPassword(student.getSid(),password);
            if (count > 0){
                return ResBean.success("修改密码成功!");
            }else{
                return ResBean.error("修改密码失败！");
            }
        }


    }

    @Override
    public List<Student> getStudentByCid(Integer cid) {
        return studentMapper.getStudentByCid(cid);
    }

    @Override
    public Integer updateTop(Integer cid, Integer sid) {
        return studentMapper.updateTop(cid, sid);
    }

    @Override
    public Integer updateNoTop(Integer cid, Integer sid) {
        return studentMapper.updateNoTop(cid,sid);
    }

    @Override
    public List<Course> getCourseTop(Integer sid) {
        return studentMapper.getCourseTop(sid);
    }

    @Override
    public List<Course> getOtherCourseTop(Integer sid) {

        return studentMapper.getOtherCourseTop(sid);
    }
}
